// Copyright 2011: Thiago Noronha (tfn@dcc.ufmg.br)

#include "chronometer.h"

#include <sys/time.h>
#include <stdlib.h>

uint64_t Chronometer::Time() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec;
}

void Chronometer::Reset() {
  time = Time();
  stopped = 0;
}

Chronometer::Chronometer() {
  Reset();
}

void Chronometer::Stop() {
  if (stopped == 0)  {
    stopped = Time();
  }
}

void Chronometer::Continue() {
  if (stopped > 0)     {
    time += Time() - stopped;
    stopped = 0;
  }
}

uint64_t Chronometer::Value() {
  return Time() - time;
}

double Chronometer::dValue() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  double ms = static_cast<double>(tv.tv_usec)/1000000.0;
  double val = (Time() - time) + ms;
  val = static_cast<int>(val*100) / 100.0;
  return val;
}
